<?php
class AlbumManagerController extends CustomControllerAction {
	protected $identity = null;
	public function init() {
		parent::init();
		
		$this->breadcrumbs->addStep('用户账户', $this->getUrl(null, 'account'));
        $this->breadcrumbs->addStep('相册管理', $this->getUrl(null, 'albummanager'));
        
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect($this->getUrl('login', 'account'));
        }
        $this->identity = $auth->getIdentity();
	}
	public function indexAction() {
		$options = array('userid' => $this->identity->userid);
		
		$albums = DatabaseObject_Album::GetAlbums($this->db, $options);
		$totalAlbum = DatabaseObject_Album::GetAlbumCount($this->db, $options);
		
		$this->view->albums = $albums;
		$this->view->totalAlbum = $totalAlbum;
	}
	
	public function uploadAction() {
		//TODO 上传
		$request = $this->getRequest();
		
		$albumid = (int) $request->getPost('id');
		$album = new DatabaseObject_Album($this->db);
		$album->load($albumid);
		
		if ($request->getPost('upload')) {
			$fp = new FormProcessor_UploadImage($this->db, $albumid);
			
			if ($fp->process($request)) {
				$this->messager->addMessage('图片已上传');
			} else {
				foreach ($fp->getError() as $error) {
					$this->messager->addMessage($error);
				}
			}
		}
		
		$options = array('userid' => $this->identity->userid);
		$albums = DatabaseObject_Album::GetAlbums($this->db, $options);
		$totalAlbum = DatabaseObject_Album::GetAlbumCount($this->db, $options);
		$url = $this->getUrl('preview').'?id='.$album->getId();
		$this->_redirect($url);
	}
	
	public function setAction() {
		$request = $this->getRequest();
		$albumid = (int) $request->getQuery('aid');
		$imageid = (int) $request->getQuery('iid');
		if ($request->getQuery('action') == 'del') {
			$image = new DatabaseObject_Image($this->db);
			if ($image->load($imageid)) {
				$image->delete();
				$this->messager->addMessage('图片已删除');
			}
		}
		
		if ($request->getQuery('action') == 'cover') {
		    $album = new DatabaseObject_Album($this->db);
            if ($album->load($albumid)) {
                $album->coverid = $imageid;
                $album->save();
                $this->messager->addMessage('相册封面已设置');
            }
		}
		
		$url = $this->getUrl('preview').'?id='.$albumid;
        $this->_redirect($url);
	}
	public function createAction() {
		//TODO 相册可见性
		$request = $this->getRequest();
		
		$fp = new FormProcessor_CreateAlbum($this->db, $this->identity->userid);
		if ($request->isPost()) {
			if ($fp->process($request)) {
				$this->messager->addMessage('相册已创建');
				$this->_redirect($this->getUrl('index'));
			}
		}
		
		$this->breadcrumbs->addStep('创建相册', $this->getUrl('create', 'albummanager'));
		
		$this->view->fp = $fp;		
	}
	
	public function previewAction() {
		$request = $this->getRequest();
		$id = (int)$request->getQuery('id');
		
		$album = new DatabaseObject_Album($this->db);
		$album->load($id);
		
		$options = array('albumid' => $id);
		$images = DatabaseObject_Image::GetImages($this->db, $options);
		$totalImage = DatabaseObject_Image::GetImageCount($this->db, $options);
		
		$url = $this->getUrl('preview').'?id='.$album->getId();
		$this->breadcrumbs->addStep('相册：'.$album->albumname, $url);
		
		$this->view->album = $album;
		$this->view->images = $images;
		$this->view->totalImage = $totalImage;
	}
}
?>